package com.dwjm.shop.utils;

import java.sql.Connection;
import java.sql.DriverManager;

public class ConnectionDatabaseForMySQLUseJdbc {
    //连接数据库的驱动
    private static final String DRIVER = "com.mysql.cj.jdbc.Driver";
    //链接数据库的URL
    private static final String DB_URL = "jdbc:mysql://localhost:3306/shopping?useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true";
    //连接数据库的账号
    private static final String USER = "root";
    //连接数据库的密码
    private static final String PASSWORD = "123456";

    //数据库连接函数
    public static Connection getConnection() {
        try{
            Class.forName(DRIVER);
            //我们期望要执行的程序，连接数据库的程序
            Connection connection = DriverManager.getConnection(DB_URL, USER, PASSWORD);
            return connection;
        }catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    //测试数据库是否可以连接
    public  static void main(String[] args) {
        System.out.println(getConnection() == null);//true -> false
    }
}
